package org.example.interceptor;


import org.example.pojo.entity.ApUser;

/**
 * 工具类：实现向threadlocal存储数据的方法
 */
public class UserHolder {

    private static ThreadLocal<ApUser> tl = new ThreadLocal<>();


    //将用户对象，存入Threadlocal
    public static void set(ApUser user) {
        System.out.println("Setting user: " + user);
        tl.set(user);
    }

    //从当前线程，获取用户对象
    public static ApUser get() {
        ApUser user = tl.get();
        System.out.println("Getting user: " + user);
        return user;
    }

    //从当前线程，获取用户对象的id
    public static Integer getUserId() {
        return tl.get().getId();
    }

}
